package com.example.fisttestproject;

import android.content.Context;

import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.example.fisttestproject.dao.UserDao;
import com.example.fisttestproject.database.AppDatabase;
import com.example.fisttestproject.entity.User;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 测试存储
 */
@RunWith(AndroidJUnit4.class)
public class SimpleEntityReadWriteTest {
    private UserDao userDao;
    private AppDatabase db;

    @Before
    public void createDb() {
        Context context = ApplicationProvider.getApplicationContext();
//        db = Room.inMemoryDatabaseBuilder(context, AppDatabase.class).build();
        db = Room.databaseBuilder(context, AppDatabase.class,"zehuatest").build();
        userDao = db.userDao();
        System.out.println("query All");
        List<User> all = userDao.getAll();
        all.stream().forEach(System.out::println);
    }

    @After
    public void closeDb() throws IOException {
        db.close();
    }

    @Test
    public void writeUserAndReadInList() throws Exception {

         db.userDao().getAll().stream().forEach(System.out::println);


        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setId(new Date().getTime());
        user.setName("test");
        userList.add(user);
//        user = new User();
//        user.setId(new Date().getTime());
//        user.setName("test2");
//        userList.add(user);
        db.userDao().insertAll(userList.toArray(new User[userList.size()]));



        List<User> all = db.userDao().getAll();
        all.stream().forEach(System.out::println);
    }
}